// +----------------------------------------------------------------------
// | BossHongshi [鸿时互娱管理系统]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.hongshihuyu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: wgh
// +----------------------------------------------------------------------
const fs = require('fs');
module.exports = class extends think.cmswing.center {
  /**
     * index action
     * 用户中心主页
     * @return {Promise} []
     */
  async indexAction() {
    // auto render template file index_index.html
    // if (!this.is_login) {
    //     return think.statusAction(700, this.http);
    // }
    // 判断是否登陆
    // await this.weblogin();
    if (!this.is_login) {
      // 判断浏览客户端
      if (this.isMobile) {
        // 手机端直接跳转到登录页面
        return this.redirect('/center/public/login');
      } else {
        return this.redirect('/cmswing/error/login');
      }
    }
    // this.http.error = new Error('成功信息！');
    // return think.statusAction(701, this.http);
    // this.http.error = new Error('错误信息！');
    // return think.statusAction(702, this.http);
    // 获取用户信息
    const userInfo = await this.model('member').join({
      table: 'member_group',
      join: 'left',
      as: 'c',
      on: ['groupid', 'groupid']
    }).find(this.user.uid);
    
    this.assign('userInfo', userInfo);
    // 总收入
    //const balance = await this.model('balance_log').where({user_id:this.user.uid,type:5}).getField('amount');
//    const incomeTotal = eval(balance.join('+'));
    console.log('userInfo.amount-s-:'+userInfo.amount);
    this.assign('incomeTotal', userInfo.amount);
    
    //待结算
    const settle = await this.model('profit_log').where({user_id:this.user.uid,status:0}).getField('amount');
    const settleTotal = eval(settle.join('+'));
    this.assign('settleTotal', settleTotal);
    
//    console.log('GetDateStr-s-:'+GetDateStr(0) + ' ' + '00:00:00');
//    console.log('GetDateStr-e-:'+GetDateStr(0) + ' ' + '23:59:59');
    //今日收入
    const map = {
    		user_id:this.user.uid
    };
    map.create_time = {'>=': new Date(GetDateStr(0) + ' ' + '00:00:00').getTime(), '<=': new Date(GetDateStr(0) + ' ' + '23:59:59').getTime()};
    const today = await this.model('profit_log').where(map).getField('amount');
    const todayTotal = eval(today.join('+'));
    this.assign('todayTotal', todayTotal);
    
    // 进行中的订单
    //const onOrder = await this.model('order').where({type:0,status:['IN', [2,4]],pay_status:['IN', [0,1]],delivery_status:['IN', [0,1]],user_id: this.user.uid}).count('id');
    
    //this.assign('onOrder', onOrder);
    // 带评价的商品 TODO
    this.meta_title = '代理中心';

    // 判断浏览客户端
    if (this.isMobile) {
      const mtype = this.get('mtype');
      if (mtype == 'vue') {
        // vue
        const vuedata = {orderTotal: orderTotal, onOrder: onOrder};
        return this.json(vuedata);
      } else { // 普通模板
        this.active = this.ctx.controller + '/' + this.ctx.action;
        return this.display(this.mtpl());
      }
    } else {
      return this.display();
    }
  }

  // 获取头像
  async avatarAction() {
    const uid = this.get('uid') || this.user.uid;
    var uploadPath = think.resource + '/upload/avatar/' + uid;
    const path = think.isFile(uploadPath + '/' + '/avatar.png');
    let pic;
    if (path) {
      pic = fs.readFileSync(uploadPath + '/' + '/avatar.png');
    } else {
      pic = fs.readFileSync(think.resource + '/upload/avatar/avatar.jpg');
    }
    this.header('Content-Type', 'image/png');
    return this.body = pic;
  }
  
};
